/**
 * @ViewToConfig.as
 * 
 * @author sodaChen mail:asframe@163.com
 * @version 1.0
 * <br>Copyright (C), 2012 ASFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame AStruts2
 * <br>Date:2012-6-16
 */
package com.asframe.astruts2.config
{
	import com.asframe.astruts2.AStrutst2Init;
	import com.asframe.astruts2.Astruts2Const;
	import com.asframe.astruts2.beans.MediatorBean;
	import com.asframe.astruts2.beans.ViewBean;
	import com.asframe.astruts2.context.AStrutsData;
	import com.asframe.astruts2.controller.InitViewMediator;
	import com.asframe.astruts2.event.InitViewMediatorEvent;
	import com.asframe.ref.Arg;
	import com.asframe.ref.Metadata;
	import com.asframe.ref.Type;
	import com.asframe.utils.ClassUtils;
	import com.asframe.utils.RefUtils;

	/**
	 * 界面对象变成配置对象
	 * @author sodaChen
	 * @Date:2012-6-16
	 */
	public class ObjectToConfigBean
	{
		public function ObjectToConfigBean()
		{
			
		}
		private function onViewInitEvent(event:InitViewMediatorEvent):void
		{
			var initViewMediator:InitViewMediator = event.target as InitViewMediator;
			initViewMediator.removeEventListener(InitViewMediatorEvent.INIT_OVER,onViewInitEvent);
		}
		/**
		 * 初始化View 
		 * @param astrutsData 框架数据对象
		 * @param viewName view的名称
		 * @param viewClass view的Class对象
		 * @param mediatorClasses mediator的Class对象数组
		 * @return 返回ViewBean的配置实例
		 * 
		 */		
		public function initViewMediorClass(	astrutsData:AStrutsData,
												viewName:String,
												viewClass:Class, 
												mediatorClasses:Array):ViewBean
		{
			var viewBean:ViewBean = viewToViewBean(viewName,viewClass);
			for (var i:int = 0; i < mediatorClasses.length; i++) 
			{
				mediatorToMediatorBean(viewBean,null,mediatorClasses[i]);
			}
			//进行初始化
			AStrutst2Init.initViewBean(viewBean,astrutsData,onViewInitEvent);
			return viewBean;
		}
		/**
		 * ViewClass对象转换成ViewBean对象(解析View的相关配置信息) 
		 * @param viewName view对象的名称
		 * @param viewClass view的Class实例
		 * @return 返回ViewBean的配置实例
		 */		
		public function viewToViewBean(viewName:String,viewClass:Class):ViewBean
		{
			var viewBean:ViewBean = new ViewBean();
			if(viewName == null)
			{
				viewName = firstBig(ClassUtils.getClassName(viewClass));
			}
			viewBean.name = viewName;
			var viewType:Type = Type.forClass(viewClass);
			viewBean.type = viewType;
			initPro(Astruts2Const.VIEW_METADATA,viewType,viewBean);
			return viewBean;
		}
		/**
		 * MediatorClass转变成MediatorBean对象
		 * @param viewBean viewBean配置信息
		 * @param mediatorName 名称
		 * @param mediatorClass mediatorClass对象
		 * 
		 */		
		public function mediatorToMediatorBean(viewBean:ViewBean,mediatorName:String,mediatorClass:Class):void
		{
			var mediatorBean:MediatorBean = new MediatorBean();
			if(mediatorName == null)
			{
				mediatorName = firstBig(ClassUtils.getClassName(mediatorClass));
			}
			mediatorBean.name = mediatorName;
			mediatorBean.mediatorClass = mediatorClass;
			var mediatorType:Type = Type.forClass(mediatorClass);
			mediatorBean.mediatorType = mediatorType;
			initPro(Astruts2Const.MEDIATOR_METADATA,mediatorType,mediatorBean);
			if(viewBean.mediators == null)
			{
				viewBean.mediators = [];
			}
			viewBean.mediators.push(mediatorBean);
		}
		/**
		 * 初始化对象的相关属性 
		 * @param metadataName 标签名称
		 * @param type 类型
		 * @param target 初始化属性的目标对象
		 * 
		 */		
		private function initPro(metadataName:String,type:Type,target:Object):void
		{
			//进行解析，是否有相关的配置信心
			var metadata:Metadata = type.getMetadata(metadataName);
			if(metadata != null)
			{
				var args:Array = metadata.args;
				var len:int = args.length;
				for (var i:int = 0; i < args.length; i++) 
				{
					var arg:Arg = args[i];
					RefUtils.setObjectValue(target,arg.key,arg.value,type);
				}
			}
		}
		private function firstBig(str:String):String
		{
			//首字母转成小写
			var first:String = str.substr(0,1).toLowerCase();
			return first + str.substr(1,str.length);
		}
	}
}
